﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
    public partial class FittingSelect : System.Windows.Forms.Form
	{
		public FittingSelect(IList<Element> collection)
		{
			this.InitializeComponent();
			this.fsCollection = collection;
			this.initForm();
		}

		private ConnectorProfileType GetConnectorProfileType(Element element)
		{
			Connector connector = null;
			Document document = element.Document;
			UnifiedModified.ActivateFamilySymbol(element as FamilySymbol);
			FamilyInstance familyInstance = document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), element as FamilySymbol, 0);
			ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
			if (connectorSetIterator.MoveNext())
			{
				connector = (connectorSetIterator.Current as Connector);
			}
			ConnectorProfileType shape = connector.Shape;
			document.Delete(familyInstance.Id);
			return shape;
		}

		private void initForm()
		{
			if (this.fsCollection != null)
			{
				foreach (Element element in this.fsCollection)
				{
					FamilySymbol familySymbol = element as FamilySymbol;
					string name = familySymbol.Family.Name;
					string name2 = familySymbol.Name;
					string key = element.Id.IntegerValue.ToString();
					if (this.tvFitting.Nodes[name] != null)
					{
						this.tvFitting.Nodes[name].Nodes.Add(key, name2);
						this.tvFitting.Nodes[name].Nodes[key].Tag = familySymbol;
					}
					else
					{
						this.tvFitting.Nodes.Add(name, name);
						this.tvFitting.Nodes[name].Tag = familySymbol;
						this.tvFitting.Nodes[name].Nodes.Add(key, name2);
						this.tvFitting.Nodes[name].Nodes[key].Tag = familySymbol;
					}
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void tvFitting_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			try
			{
				FamilySymbol familySymbol = e.Node.Tag as FamilySymbol;
				this.familySymbol = familySymbol;
				Bitmap previewImage = familySymbol.GetPreviewImage(new Size(250, 250));
				this.picBoxpreview.Image = previewImage;
			}
			catch (Exception)
			{
			}
		}

		private IList<Element> fsCollection = new List<Element>();

		public FamilySymbol familySymbol;
	}
}
